<?php

/**
 * ClassName:评论类
 * @auditor LeiJunXiang
 * @time 2024/11/15
 */

namespace app\model\invite;

use app\factory\Factory;
use app\model\ModelBase;
use app\modelOpreate\invite\CommentOpe;
use app\modelOpreate\user\ForumUserOpe;

class Comment extends ModelBase
{
    const MODEL_COMMENT = "Comment";
    //评论编号
    protected $count;
    //评论内容
    protected $commentContent;
    //评论用户id
    protected $commentUserId;
    //评论时间
    protected $commentTime;
    //父评论对象key
    protected $fatherObjKey;
    //点赞
    protected $like;
    //是否有子评论
    protected $isChildComment;
    //是否发布
    protected $isPublish;
    //是否删除
    protected $isDelete;

    public function __construct()
    {
        $this->like = 0;
        $this->isChildComment = 0;
    }

    /**
     * 创建对象
     * @return
     */
    static public function createObj()
    {
        $count = Factory::createUniqueNumber();
        $obj = Factory::getInstance()->getModelObj(Comment::MODEL_COMMENT, $count);
        $obj->setCount($count);
        return $obj;
    }


    /**
     * 查询评论（父对象key，页数，条数，展示子评论条数）
     */
    public function selectComment($inviteObjKey, $page, $limit, $showChildCommentNum)
    {
        // 计算查询的起始位置
        $offset = ($page - 1) * $limit;

        $tool = Factory::getInstance()->getToolObj(CommentOpe::TOOL_COMMENT_OPE);
        //查询评论(对象key,起始位置，条数)
        $comments = $tool->selectComment($inviteObjKey, $offset, $limit);
        if ($comments) {
            //查询子评论
            foreach ($comments as &$comment) {
                if ($comment['isChildComment'] == 0) {
                    $comment['commentCount'] = 0;
                    continue;
                }
                //父评论对象key
                $fatherObjKey = $comment['objKey'];
                //查询子评论总数
                $comment['commentCount'] = $tool->selectCommentCount($fatherObjKey);
                //查询评论
                $arr = $this->selectChildComment($fatherObjKey, $showChildCommentNum);
                //评论对象队列
                $comment['commentCountList'] = $arr;
            }
        }
        return $comments;
    }


    /**
     * 删除评论(用户id,评论对象key)
     */
    public function delComment($userId, $commentObjKey)
    {
        $tool = Factory::getInstance()->getToolObj(CommentOpe::TOOL_COMMENT_OPE);
        $flag = $tool->delComment($userId, $commentObjKey);
        return $flag;
    }

    /**
     * 点赞评论(对象key)
     */
    public function likeComment($commentObjKey)
    {
        $tool = Factory::getInstance()->getToolObj(CommentOpe::TOOL_COMMENT_OPE);
        $flag = $tool->likeComment($commentObjKey);
        return $flag;
    }


    /**
     * 查询子评论
     * @return void
     */
    protected function selectChildComment($fatherObjKey, $showChildCommentNum, $count = 1)
    {
        if ($count >= 2) {
            return [];
        }
        $tool = Factory::getInstance()->getToolObj(CommentOpe::TOOL_COMMENT_OPE);
        //查询评论(对象key,起始位置，条数)
        $childComments = $tool->selectComment($fatherObjKey, 0, $showChildCommentNum);
        $count++;
        if (empty($childComments)) {
            return [];
        }
        foreach ($childComments as &$childComment) {
            if ($childComment['isChildComment'] == 0) {
                $childComment['commentCount'] = 0;
                continue;
            }
            //父评论对象key
            $fatherObjKey = $childComment['objKey'];
            //查询子评论总数
            $childComment['commentCount'] = $tool->selectCommentCount($fatherObjKey);
            //子评论对象
            $arr = $this->selectChildComment($fatherObjKey, $showChildCommentNum, $count);
            $childComment['commentCountList'] = $arr;
        }
        return $childComments;
    }

    /**
     * 发布评论
     */
    public function publishComment()
    {
        $tool = Factory::getInstance()->getToolObj(CommentOpe::TOOL_COMMENT_OPE);
        $arr = $this->getAttribute();
        $flag = $tool->save($arr);
        return $flag;
    }


    /**
     * @return mixed
     */
    public function getCount()
    {
        return $this->count;
    }

    /**
     * @param mixed $count
     */
    public function setCount($count): void
    {
        $this->count = $count;
    }

    /**
     * @return mixed
     */
    public function getCommentContent()
    {
        return $this->commentContent;
    }

    /**
     * @param mixed $commentContent
     */
    public function setCommentContent($commentContent): void
    {
        $this->commentContent = $commentContent;
    }


    /**
     * @return mixed
     */
    public function getCommentUserId()
    {
        return $this->commentUserId;
    }

    /**
     * @param mixed $commentUserId
     */
    public function setCommentUserId($commentUserId): void
    {
        $this->commentUserId = $commentUserId;
    }

    /**
     * @return mixed
     */
    public function getCommentTime()
    {
        return $this->commentTime;
    }

    /**
     * @param mixed $commentTime
     */
    public function setCommentTime($commentTime): void
    {
        $this->commentTime = $commentTime;
    }

    /**
     * @return mixed
     */
    public function getFatherObjKey()
    {
        return $this->fatherObjKey;
    }

    /**
     * @param mixed $fatherObjKey
     */
    public function setFatherObjKey($fatherObjKey): void
    {
        $this->fatherObjKey = $fatherObjKey;
    }

    /**
     * @return mixed
     */
    public function getLike()
    {
        return $this->like;
    }

    /**
     * @param mixed $like
     */
    public function setLike($like): void
    {
        $this->like = $like;
    }

    /**
     * @return mixed
     */
    public function getIsPublish()
    {
        return $this->isPublish;
    }

    /**
     * @param mixed $isPublish
     */
    public function setIsPublish($isPublish): void
    {
        $this->isPublish = $isPublish;
    }

    public function getIsChildComment(): int
    {
        return $this->isChildComment;
    }

    public function setIsChildComment(int $isChildComment): void
    {
        $this->isChildComment = $isChildComment;
    }

    /**
     * @return mixed
     */
    public function getIsDelete()
    {
        return $this->isDelete;
    }

    /**
     * @param mixed $isDelete
     */
    public function setIsDelete($isDelete): void
    {
        $this->isDelete = $isDelete;
    }


}